package com.debao.web.controller.system;

import java.util.*;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.debao.common.annotation.Anonymous;
import com.debao.common.constant.CacheConstants;
import com.debao.common.core.redis.RedisCache;
import com.debao.common.utils.AliyunSmsUtil;
import com.debao.common.utils.KeyGreaterUtil;
import com.debao.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.debao.common.constant.Constants;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.core.domain.entity.SysMenu;
import com.debao.common.core.domain.entity.SysUser;
import com.debao.common.core.domain.model.LoginBody;
import com.debao.common.utils.SecurityUtils;
import com.debao.framework.web.service.SysLoginService;
import com.debao.framework.web.service.SysPermissionService;
import com.debao.system.service.ISysMenuService;

import javax.annotation.Resource;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RedisCache redisCache;


    /**
     * 登录方法
     * 
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        Long userId = SecurityUtils.getUserId();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(userId);
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /**
     * 发送手机验证码
     */
    @PostMapping("/sendPhone/{phone}")
    @Anonymous
    public AjaxResult sendPhone(@PathVariable("phone") String phone) throws ClientException {
        //生成6为短信验证码
        String code = KeyGreaterUtil.getRandom620(6);
        //短信验证码存入Redis中
        redisCache.setCacheObject(CacheConstants.SMS_CODE + phone, code, Constants.SMS_EXPIRATION, TimeUnit.MINUTES);
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("code", code);
        //发送短信验证码
        AliyunSmsUtil.sendSms(phone, "SMS_468980633", JSONObject.toJSONString(paramMap));
        System.out.println("短信验证码：" + code);
        return AjaxResult.success("短信验证码发送成功");
    }
}
